import axios from "axios"; //引入axios
import type { AxiosInstance, AxiosResponse } from "axios";
import QS from "qs"; // 引入qs模块，用来序列化post类型的数据
import { ElMessage } from "element-plus";
import type { requeryRes } from "@/common/interface";
import config from "./config";
import router from "@/router"


// 创建axios实例
const instance: AxiosInstance = axios.create({
  timeout: 8000, // 请求超时时间
});

instance.defaults.baseURL = config.baseUrl; //请求环境

// 设置 post默认 Content-Type
// instance.defaults.headers.post["Content-Type"] =
//   "application/json;charset=UTF-8";

// 设置请求拦截器
instance.interceptors.request.use(
  (config: any) => {
    config.headers.token = localStorage.getItem("token") || "";
    config.headers.client = "admin";
    config.headers.lang = localStorage.getItem("lang") || "zh";

    return config;
  },
  (err) => {
    // 请求错误
    return Promise.reject(err);
  }
);

// 设置响应拦截器
instance.interceptors.response.use(
  (response: AxiosResponse) => {
    if (response.status == 200) {
      if (response.data.code === 401) {
        localStorage.removeItem("admin");
        localStorage.removeItem("token");
        if (config.dev) {
          location.href = "/login";
        } else {
          location.href = config.loginPath;
        }
      }
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  (error) => {
    // 超时处理
    let originalRequest = error.config;
    if (
      error.code === "ECONNABORTED" &&
      error.message.indexOf("timeout") !== -1 &&
      !originalRequest._retry
    ) {
      ElMessage({
        type: "error",
        message: "请检查网络再重新连接",
      });
      return Promise.reject("请检查网络再重新连接");
    }
    if (error.response.status) {
      return Promise.reject(error.response);
    }
  }
);

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url: string, params?: any) {
  return new Promise<requeryRes>((resolve: any, reject: any) => {
    instance
      .get(url, {
        params: params,
      })
      .then((res: any) => {
        resolve(res.data);
      })
      .catch((err: any) => {
        reject({
          code: 400,
          data: err,
        });
      });
  });
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url: string, params?: any) {
  return new Promise<requeryRes>((resolve: any, reject: any) => {
    instance
      .post(url, QS.stringify(params))
      .then((res: any) => {
        resolve(res.data);
      })
      .catch((err: any) => {
        reject({
          code: 400,
          data: err,
        });
      });
  });
}
